import Vue from "vue";
import axios from "axios"
import router from "../../router/index";
// import { baseURL } from '../config/basic'
const baseURL = process.env.VUE_APP_baseURL;
var instance = axios.create({
    baseURL: baseURL,
    timeout: 10000,
    headers: {
        "Content-Type": "application/json",
    },
});
// 响应
instance.interceptors.response.use(res => {
    return res.data;
}, error => {
    if (error.response.status === 403) {
        router.push('/login');
        return;
    }
    return error;
});

// 请求
instance.interceptors.request.use(config => {
    const token = localStorage.token;// 以后如何要用到vux可以修改
    token && (config.headers.Authorization = 'Bearer ' + token);
    return config;
}, error => {
    return Promise.error(error);
});


const $get = (url, params) => {
    return instance.get(url, {
        params
    })
}
const $post = (url, body) => {
    return instance.post(url, body)
}

Vue.prototype.$get = $get;
Vue.prototype.$post = $post;
export {
    $get, $post
}